package com.quectel.business.common.help;

import cn.hutool.core.lang.func.LambdaUtil;
import com.quectel.constant.core.help.FeedBackConstants;
import com.quectel.core.module.dept.dto.DeptPersonDto;
import com.quectel.core.module.dept.service.DeptPersonService;
import com.quectel.core.module.help.dto.FeedbackDto;
import com.quectel.core.module.household.dto.HouseholdDto;
import com.quectel.core.module.household.service.HouseholdService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 在这里编写说明
 *
 * @author: bob
 * @email: bob.yu@quectel.com
 * @date: 2023-05-05 13:32:00
 */
@Component
@Slf4j
public class FeedbackBusiness {

    @Autowired
    private HouseholdService householdService;
    @Autowired
    private DeptPersonService deptPersonService;

    /**
     * 通过名称搜索住户端 + 员工端
     * 查询返回用户id列表
     */
    public List<Long> getIdList(String name){
        List<Long> ids = new ArrayList<>();

        // 搜索deptPerson
        Map<String,Object> queryDeptPersonMap = new HashMap<>();
        queryDeptPersonMap.put(LambdaUtil.getFieldName(DeptPersonDto::getName), name);
        List<DeptPersonDto> deptPersonDtos = deptPersonService.queryList(queryDeptPersonMap);
        if (CollectionUtils.isNotEmpty(deptPersonDtos)){
            List<Long> deptPersonIds = deptPersonDtos.stream()
                    .map(DeptPersonDto::getId).collect(Collectors.toList());
            ids.addAll(deptPersonIds);
        }

        Map<String,Object> householdSqlMap = new HashMap<>();
        householdSqlMap.put(LambdaUtil.getFieldName(HouseholdDto::getName), name);
        // 搜索mobileUser
        List<HouseholdDto> householdDtos = householdService.queryList(householdSqlMap);
        if (CollectionUtils.isNotEmpty(householdDtos)){
            List<Long> householdIds = householdDtos.stream()
                    .map(HouseholdDto::getId).collect(Collectors.toList());
            ids.addAll(householdIds);
        }
        return ids;
    }

    public String getPersonName(FeedbackDto feedbackDto){
        Byte personType = feedbackDto.getPersonType();
        String personName = null;
        if (FeedBackConstants.PersonType.HOUSEHOLD.getType().equals(personType)){
            HouseholdDto householdDto = householdService.selectCacheById(feedbackDto.getPersonId());
            if (householdDto!= null){
                personName = householdDto.getName();
            }
        }

        if (FeedBackConstants.PersonType.EMP.getType().equals(personType)){
            DeptPersonDto deptPersonDto = deptPersonService.selectCacheById(feedbackDto.getPersonId());
            if (deptPersonDto!= null){
                personName = deptPersonDto.getName();
            }
        }
        return personName;
    }

}
